<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 18-3-12
 * Time: 上午9:53
 */

namespace api\modules\chedai\controllers;


use api\modules\chedai\tools\ApiTools;
use common\components\tools\Tools;
use common\models\credit_app\OrderCert;
use wodrow\yii\rest\Controller;
use yii\base\ErrorException;
use yii\db\Exception;

class CertController extends Controller
{
    /**
     * 保存征信
     * @desc post 如果有id，则修改，没有则新建，新建必须有order_id
     * @param int $id 征信号
     * @param int $order_id 订单号
     */
    public function actionSave($id = null, $order_id = null)
    {
        $x = [
//            OrderCert::class,
            OrderCert::className(),
        ];
        return $x;
    }

    /**
     * 保存本人征信
     * @desc post 如果没有本人征信则新建本人的征信
     * @param int $order_id 客户订单号
     */
    public function actionSaveVip($order_id)
    {
        $order = $this->_getOrder($order_id);
        if (!$order){
            throw new ErrorException("没有找到客户订单!");
        }
        $cert_vip = $order->certVip;
        if (!$cert_vip){
            $cert_vip = new OrderCert();
            $cert_vip->ct_oid = $order->o_id;
        }
        $x = ApiTools::setSerializeApiField(\Yii::$app->request->post(), OrderCert::$serialize_fields);
        $cert_vip->load($x, '');
        $x = $cert_vip->save();
        if (!$x){
            throw new Exception(Tools::makeModelGetErrorsToStringAndGetIt($cert_vip));
        }
        $o['cert_vip'] = ApiTools::showSerializeApiField($cert_vip->toArray(), OrderCert::$serialize_fields);
        return $o;
    }

    /**
     * 保存配偶征信
     * @desc post 如果没有配偶征信则新建配偶的征信
     * @param int $order_id 客户订单号
     */
    public function actionSaveSpouse($order_id)
    {
        $order = $this->_getOrder($order_id);
        if (!$order){
            throw new ErrorException("没有找到客户订单!");
        }
        $cert_spouse = $order->certSpouse;
        if (!$cert_spouse){
            $cert_spouse = new OrderCert();
            $cert_spouse->ct_oid = $order->o_id;
        }
        $x = ApiTools::setSerializeApiField(\Yii::$app->request->post(), OrderCert::$serialize_fields);
        $cert_spouse->load($x, '');
        $x = $cert_spouse->save();
        if (!$x){
            throw new Exception(Tools::makeModelGetErrorsToStringAndGetIt($cert_spouse));
        }
        $o['cert_spouse'] = ApiTools::showSerializeApiField($cert_spouse->toArray(), OrderCert::$serialize_fields);
        return $o;
    }

    /**
     * 保存担保人征信
     * @desc 一般ct开头
     * @param int $order_id 客户订单号
     * @param $cert_id
     */
    public function actionSaveGuarantor($order_id, $cert_id)
    {
        $cert_guarantor = OrderCert::findOne(['ct_id'=>$cert_id, 'ct_key'=>OrderCert::KEY_GUARANTOR]);
        if (!$cert_guarantor){
            $cert_guarantor = new OrderCert();
            $cert_guarantor->ct_oid = $order_id;
            $cert_guarantor->ct_key = OrderCert::KEY_GUARANTOR;
        }else{
            if ($cert_guarantor->order->o_id!=$order_id){
                throw new ErrorException('存在的担保人征信与订单号不匹配!');
            }
        }
        $x = ApiTools::setSerializeApiField(\Yii::$app->request->post(), OrderCert::$serialize_fields);
        $cert_guarantor->load($x, '');
        $x = $cert_guarantor->save();
        if (!$x){
            throw new Exception(Tools::makeModelGetErrorsToStringAndGetIt($cert_guarantor));
        }
        $o['cert_guarantor'] = ApiTools::showSerializeApiField($cert_guarantor->toArray(), OrderCert::$serialize_fields);
        return $o;
    }

    private function _getOrder($order_id)
    {
        return \common\models\credit_app\Order::findActiveOneById($order_id);
    }
}